package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.mapper.UserMapper;
import com.usian.pojo.User;
import com.usian.query.UserDto;
import com.usian.service.UserService;
import com.usian.utils.JWTUtils;
import com.usian.utils.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService  {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectById(id);
    }


    @Override
    public PageResult<User> findPage(UserDto dto) {
        Page<User> pageParam =new Page<User>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper =new LambdaQueryWrapper<>();

        if(StringUtils.isNotBlank(dto.getUsername())){
            lambdaQueryWrapper.like(User::getUsername,dto.getUsername());
        }

        IPage<User> page =page(pageParam,lambdaQueryWrapper);

        PageResult<User> result =new PageResult<>();
        result.setTotal(page.getTotal());
        result.setRows(page.getRecords());

        return result;
    }

    @Override
    public boolean saveUser(User user) {
        return this.save(user);
    }

    @Override
    public boolean updateUser(User user) {
        return this.updateById(user);
    }

    @Override
    public boolean deleteById(Integer id) {
        return this.removeById(id);
    }

    @Override
    public User login(User user) {

        /**
         * 1: 参数判空
         * 2：查询用户  user  null
         * 3：生成token
         * 4: token 带回去  web-->gateway-->consumer--->provider
         * 5: redis 存token 有效期  key token  value user exprims 5分钟
         */

        User user1 =new User();
        if(user!=null){

            LambdaQueryWrapper<User> lambdaQueryWrapper =new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getUsername,user.getUsername());
            lambdaQueryWrapper.eq(User::getPassword,user.getPassword());
            user1= userMapper.selectOne(lambdaQueryWrapper);
            if(user1!=null){

                //生成token
                Map map =new HashMap<>();
                map.put("id",user1.getId().toString());
                map.put("username",user1.getUsername());
                String token = JWTUtils.getToken(map);

                // 存redis
                redisTemplate.boundValueOps(token).set(user1.getId().toString());
                redisTemplate.boundValueOps(token).expire(1, TimeUnit.MINUTES);
                user1.setToken(token);
            }
        }
        return user1;
    }


}
